<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="玖忆;文鹤;博客">
    
    <meta name="author" content="meteor">
    <!-- preconnect -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    
    
    <!--- Seo Part-->
    
    <link rel="canonical" href="https://wait-you.github.io/2023/06/06/node-js/"/>
    <meta name="robots" content="index,follow">
    <meta name="googlebot" content="index,follow">
    <meta name="revisit-after" content="1 days">
    
        <meta name="description" content="# Node.js # 概念   一句话介绍  Node.js 是一个基于 Chrome V8  引擎的 JavaScript	 运行时    js 运行时，也叫 js 执行环境  编写 js 代码 -&gt; html -&gt; 浏览器  浏览器为什么可以解析一个 网页？  UI 渲染引擎 JS 执行引擎        主要目的  是为了让 js 在其他地方执行，应对各种业务  web 服务开">
<meta property="og:type" content="article">
<meta property="og:title" content="Node.js">
<meta property="og:url" content="https://wait-you.github.io/2023/06/06/Node-js/index.html">
<meta property="og:site_name" content="玖忆">
<meta property="og:description" content="# Node.js # 概念   一句话介绍  Node.js 是一个基于 Chrome V8  引擎的 JavaScript	 运行时    js 运行时，也叫 js 执行环境  编写 js 代码 -&gt; html -&gt; 浏览器  浏览器为什么可以解析一个 网页？  UI 渲染引擎 JS 执行引擎        主要目的  是为了让 js 在其他地方执行，应对各种业务  web 服务开">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-06-06T01:02:34.000Z">
<meta property="article:modified_time" content="2023-06-05T09:02:59.926Z">
<meta property="article:author" content="meteor">
<meta property="article:tag" content="Node.js">
<meta name="twitter:card" content="summary">
    
    
    <!--- Icon Part-->
    <link rel="icon" type="image/png" href="/images/wenhe.png" sizes="192x192">
    <link rel="apple-touch-icon" sizes="180x180" href="/images/wenhe.png">
    <meta name="theme-color" content="#f1404b">
    <link rel="shortcut icon" href="/images/wenhe.png">
    <!--- Page Info-->
    
    <title>
        
            Node.js -
        
        玖忆
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    
<link rel="stylesheet" href="/assets/fonts.css">

    <!--- Font Part-->
    
    
    
    

    <!--- Inject Part-->
    
    <script id="hexo-configurations">
    let Global = window.Global || {};
    Global.hexo_config = {"hostname":"wait-you.github.io","root":"/","language":"zh-CN"};
    Global.theme_config = {"articles":{"style":{"font_size":"16px","line_height":1.5,"image_border_radius":"14px","image_alignment":"center","image_caption":false,"link_icon":true},"word_count":{"enable":true,"count":true,"min2read":true},"author_label":{"enable":true,"auto":false,"list":[]},"code_block":{"copy":true,"style":"mac","font":{"enable":false,"family":null,"url":null}},"toc":{"enable":true,"max_depth":3,"number":false,"expand":true,"init_open":true},"copyright":true,"lazyload":true,"recommendation":{"enable":false,"title":"推荐阅读","limit":3,"placeholder":"http://tuchuang.wenhe9.cn/default-bg.jpg","skip_dirs":[]}},"colors":{"primary":"#f1404b","secondary":null},"global":{"fonts":{"chinese":{"enable":false,"family":null,"url":null},"english":{"enable":false,"family":null,"url":null}},"content_max_width":"1000px","sidebar_width":"210px","hover":{"shadow":true,"scale":false},"scroll_progress":{"bar":true,"percentage":true},"busuanzi_counter":{"enable":true,"site_pv":true,"site_uv":true,"post_pv":true},"pjax":true,"open_graph":true,"google_analytics":{"enable":false,"id":null}},"home_banner":{"enable":true,"style":"static","image":{"light":"http://tuchuang.wenhe9.cn/default-bg.jpg","dark":"http://tuchuang.wenhe9.cn/default-bg.jpg"},"title":"玖忆","subtitle":{"text":["我本微末凡尘、可也心向天空"],"hitokoto":{"enable":false,"api":"https://v1.hitokoto.cn"},"typing_speed":100,"backing_speed":80,"starting_delay":500,"backing_delay":1500,"loop":true,"smart_backspace":true},"text_color":{"light":"#fff","dark":"#d1d1b6"},"text_style":{"title_size":"2.8rem","subtitle_size":"1.5rem","line_height":1.2},"custom_font":{"enable":false,"family":null,"url":null},"social_links":{"enable":true,"links":{"github":"https://gitee.com/du-jinliang","instagram":null,"zhihu":null,"twitter":null,"email":"dujinliang9@163.com"}}},"plugins":{"feed":{"enable":false},"aplayer":{"enable":false,"type":"fixed","audios":[{"name":null,"artist":null,"url":null,"cover":null}]},"mermaid":{"enable":false,"version":"9.3.0"}},"version":"2.1.4","navbar":{"auto_hide":true,"color":{"left":"#f78736","right":"#367df7","transparency":35},"links":{"Home":{"path":"/","icon":"fa-regular fa-house"}},"search":{"enable":false,"preload":true}},"page_templates":{"friends_column":2,"tags_style":"blur"},"home":{"sidebar":{"enable":true,"position":"left","first_item":"menu","announcement":null,"links":null},"article_date_format":"auto","categories":{"enable":true,"limit":3},"tags":{"enable":true,"limit":3}}};
    Global.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
    Global.data_config = {"masonry":false};
  </script>
    
    <!--- Fontawesome Part-->
    
<link rel="stylesheet" href="/fontawesome/fontawesome.min.css">

    
<link rel="stylesheet" href="/fontawesome/brands.min.css">

    
<link rel="stylesheet" href="/fontawesome/solid.min.css">

    
<link rel="stylesheet" href="/fontawesome/regular.min.css">

    
    
    
    
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
        <span class="pjax-progress-bar"></span>
        <span class="pjax-progress-icon">
            <i class="fa-solid fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container">

    

    <div class="main-content-container">

        <div class="main-content-header">
            <header class="navbar-container">
    
    <div class="navbar-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                
                玖忆
                
            </a>
        </div>

        <div class="right">
            <!-- PC -->
            <div class="desktop">
                <ul class="navbar-list">
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/"  >
                                    
                                        
                                            <i class="fa-regular fa-house"></i>
                                        
                                        首页
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                    
                </ul>
            </div>
            <!-- Mobile -->
            <div class="mobile">
                
                <div class="icon-item navbar-bar">
                    <div class="navbar-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Mobile drawer -->
    <div class="navbar-drawer">
        <ul class="drawer-navbar-list">
            
                
                    <li class="drawer-navbar-item flex-center">
                        <a class="" 
                        href="/"  >
                             
                                
                                    <i class="fa-regular fa-house"></i>
                                
                                首页
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            

        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="main-content-body">

            

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="post-page-container">
        <div class="article-content-container">

            
            
                <div class="article-title">
                    <h1 class="article-title-regular">Node.js</h1>
                </div>
            
                
            

            
                <div class="article-header">
                    <div class="avatar">
                        <img src="/images/wenhe.png">
                    </div>
                    <div class="info">
                        <div class="author">
                            <span class="name">meteor</span>
                            
                                <span class="author-label">Lv3</span>
                            
                        </div>
                        <div class="meta-info">
                            <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fa-regular fa-pen-fancy"></i>&nbsp;
        <span class="desktop">2023-06-05 17:02:34</span>
        <span class="mobile">2023-06-05 17:02</span>
        <span class="hover-info">创建</span>
    </span>
    
        <span class="article-date article-meta-item">
            <i class="fa-regular fa-wrench"></i>&nbsp;
            <span class="desktop">2023-06-05 01:02:59</span>
            <span class="mobile">2023-06-05 01:02</span>
            <span class="hover-info">更新</span>
        </span>
    

    
        <span class="article-categories article-meta-item">
            <i class="fa-regular fa-folders"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/Node-js/">Node.js</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fa-regular fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/Node-js/">Node.js</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
        <span class="article-pv article-meta-item">
            <i class="fa-regular fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                        </div>
                    </div>
                </div>
            

            <div class="article-content markdown-body">
                <h1 id="nodejs"><a class="markdownIt-Anchor" href="#nodejs">#</a> Node.js</h1>
<h2 id="概念"><a class="markdownIt-Anchor" href="#概念">#</a> 概念</h2>
<ul>
<li>
<p>一句话介绍</p>
<ul>
<li>Node.js 是一个基于 <code>Chrome V8</code>  引擎的 <code>JavaScript	</code> 运行时</li>
</ul>
</li>
<li>
<p>js 运行时，也叫 js 执行环境</p>
<ul>
<li>编写 js 代码 -&gt; html -&gt; 浏览器
<ul>
<li>浏览器为什么可以解析一个 网页？
<ul>
<li>UI 渲染引擎</li>
<li>JS 执行引擎</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>
<p>主要目的</p>
<ul>
<li>是为了让 js 在其他地方执行，应对各种业务
<ul>
<li>web 服务开发
<ul>
<li>游戏服务器</li>
<li>接口服务器</li>
<li>…</li>
</ul>
</li>
<li>构建工具</li>
<li>命令行工具
<ul>
<li>npm</li>
<li>git</li>
<li>…</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>
<p>node 相当于将引擎单独封装，所以他可以识别 es 语法部分，但无法识别 DOM 和 BOM</p>
</li>
</ul>
<h2 id="初体验"><a class="markdownIt-Anchor" href="#初体验">#</a> 初体验</h2>
<ul>
<li>
<p>准备代码</p>
<ul>
<li>
<p>创建一个单独的 js 文件</p>
</li>
<li>
<p>代码内容</p>
<ul>
<li>
<div class="highlight-container" data-rel="Js"><figure class="iseeu highlight js"><figcaption data-lang="JavaScript"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">let</span> content <span class="token operator">=</span> <span class="token string">'hello world'</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">0</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="版本管理nvm"><a class="markdownIt-Anchor" href="#版本管理nvm">#</a> 版本管理 NVM</h2>
<h3 id="常用命令"><a class="markdownIt-Anchor" href="#常用命令">#</a> 常用命令</h3>
<ul>
<li>nvm 操作
<ul>
<li>查看版本
<ul>
<li><code>nvm -v</code></li>
</ul>
</li>
<li>查看 / 設置根路徑
<ul>
<li><code>nvm root [path]</code></li>
</ul>
</li>
</ul>
</li>
<li>Node.js 版本操作
<ul>
<li>查看全部安裝版本
<ul>
<li><code>nvm ls</code></li>
</ul>
</li>
<li>查看全部可安裝版本
<ul>
<li><code>nvm ls-remote</code></li>
</ul>
</li>
<li>安裝指定版本 Node.js
<ul>
<li><code>nvm install &lt;version&gt;</code></li>
</ul>
</li>
<li>卸載指定版本 Node.js
<ul>
<li><code>nvm uninstall &lt;version&gt;</code></li>
</ul>
</li>
<li>安裝最新長期支持版本
<ul>
<li><code>nvm install --lts</code></li>
</ul>
</li>
<li>切換指定版本 Node.js
<ul>
<li><code>nvm use node &lt;version&gt;</code></li>
</ul>
</li>
<li>查看當前使用的 Node.js 版本
<ul>
<li><code>nvm current</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="node全局的變量"><a class="markdownIt-Anchor" href="#node全局的變量">#</a> Node 全局的變量</h2>
<h3 id="概念-2"><a class="markdownIt-Anchor" href="#概念-2">#</a> 概念</h3>
<ul>
<li>全局环境可直接调用的变量</li>
</ul>
<h3 id="常用全局变量"><a class="markdownIt-Anchor" href="#常用全局变量">#</a> 常用全局变量</h3>
<ul>
<li>console
<ul>
<li>log
<ul>
<li>日志</li>
</ul>
</li>
<li>warn
<ul>
<li>警告</li>
</ul>
</li>
<li>time + timeEnd
<ul>
<li>定义相同的标签，计算标签中间的代码的执行时间</li>
</ul>
</li>
</ul>
</li>
<li>_dirname
<ul>
<li>当前文件所在目录的绝对路径</li>
</ul>
</li>
<li>_filename
<ul>
<li>当前文件所在的绝对路径</li>
</ul>
</li>
<li>setInterval(callback, dealay[,…args])
<ul>
<li>定时器</li>
<li>clearInterval (intervalObject) 清除定时器</li>
</ul>
</li>
<li>setTimeout(callback, delay[,…args])
<ul>
<li>计时器</li>
<li>clearTimeout (timeoutObject) 清除计时器</li>
</ul>
</li>
<li>export</li>
<li>module</li>
<li>require()</li>
</ul>
<h2 id="模块和包的概念"><a class="markdownIt-Anchor" href="#模块和包的概念">#</a> 模块和包的概念</h2>
<h3 id="模块的概念"><a class="markdownIt-Anchor" href="#模块的概念">#</a> 模块的概念</h3>
<ul>
<li>
<p>将一组相关的 API 封装在同一个文件内部，供外界引入使用，这个文件被称为 “模块”</p>
</li>
<li>
<p>不同的模块有不同的名称</p>
<ul>
<li>
<div class="highlight-container" data-rel="Js"><figure class="iseeu highlight js"><figcaption data-lang="JavaScript"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">class</span> <span class="token class-name">Level1</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre>    <span class="token keyword">static</span> <span class="token function-variable function">jia</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">n1<span class="token punctuation">,</span> n2</span><span class="token punctuation">)</span> <span class="token operator">=></span> n1<span class="token operator">+</span>n2</pre></td></tr><tr><td data-num="3"></td><td><pre></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token keyword">static</span> <span class="token function-variable function">jian</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">n1<span class="token punctuation">,</span> n2</span><span class="token punctuation">)</span> <span class="token operator">=></span> n1<span class="token operator">-</span>n2</pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token keyword">class</span> <span class="token class-name">Level2</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token keyword">static</span> <span class="token function-variable function">cheng</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">n1<span class="token punctuation">,</span> n2</span><span class="token punctuation">)</span> <span class="token operator">=></span> n1<span class="token operator">*</span>n2</pre></td></tr><tr><td data-num="9"></td><td><pre></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token keyword">static</span> <span class="token function-variable function">chu</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">n1<span class="token punctuation">,</span> n2</span><span class="token punctuation">)</span> <span class="token operator">=></span> n1<span class="token operator">/</span>n2</pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="12"></td><td><pre></pre></td></tr><tr><td data-num="13"></td><td><pre>module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="14"></td><td><pre>    Level1<span class="token punctuation">,</span></pre></td></tr><tr><td data-num="15"></td><td><pre>    Level2</pre></td></tr><tr><td data-num="16"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>code￼<span class="token number">1</span><span class="token operator">--</span><span class="token operator">></span></pre></td></tr></table></figure></div></li>
<li>
<div class="highlight-container" data-rel="Js"><figure class="iseeu highlight js"><figcaption data-lang="JavaScript"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">const</span> <span class="token punctuation">&#123;</span>Level1<span class="token punctuation">,</span> Level2<span class="token punctuation">&#125;</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./01-math'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token keyword">const</span> <span class="token punctuation">&#123;</span>gDate<span class="token punctuation">&#125;</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./02-date'</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre></pre></td></tr><tr><td data-num="4"></td><td><pre>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Level1<span class="token punctuation">.</span><span class="token function">jia</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="5"></td><td><pre>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Level2<span class="token punctuation">.</span><span class="token function">cheng</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>gDate<span class="token punctuation">.</span><span class="token function">gDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr></table></figure></div></li>
</ul>
</li>
</ul>
<h3 id="包的概念"><a class="markdownIt-Anchor" href="#包的概念">#</a> 包的概念</h3>
<ul>
<li>
<p>有些功能的实现，并不能单独的由一个模块实现，需要多个相关模块的配合</p>
</li>
<li>
<p>把这些相关的模块，放在同一个文件夹内部，再辅助以描述文件，形成了包的概念</p>
</li>
<li>
<p>描述文件：  <code>package.json</code></p>
<ul>
<li>
<table>
<thead>
<tr>
<th>属性名</th>
<th>描述</th>
<th>示例</th>
</tr>
</thead>
<tbody>
<tr>
<td>name</td>
<td>项目名称</td>
<td>“itlike”</td>
</tr>
<tr>
<td>version</td>
<td>版本号（大版本，中版本，小版本）</td>
<td>“1.1.0”</td>
</tr>
<tr>
<td>description</td>
<td>项目功能描述</td>
<td>“纯属测试，没啥意义”</td>
</tr>
<tr>
<td>keywords</td>
<td>项目关键词，发布后供用户检索用</td>
<td>[“撩课”, “SZ”, “Node”]</td>
</tr>
<tr>
<td>homepage</td>
<td>项目主页地址</td>
<td>“<a class="link"   target="_blank" rel="noopener" href="http://wenhe9.cn" >http://wenhe9.cn <i class="fa-regular fa-arrow-up-right-from-square fa-sm"></i></a>”</td>
</tr>
<tr name="" :="" 撩课-Sz,="" email="" :="" itlke-sz@vx.com="">
<td>author</td>
<td>项目作者</td>
<td></td>
</tr>
<tr>
<td>main</td>
<td>项目主文件</td>
<td>“index.js”</td>
</tr>
<tr like_start="" :="" nodeindex.js="">
<td>scripts</td>
<td>可用脚本 (npm run 名称)</td>
<td></td>
</tr>
<tr axios="" :="" 0.21.1="">
<td>dependencies</td>
<td>生产环境依赖的包</td>
<td></td>
</tr>
<tr>
<td>devDependencies</td>
<td>开发环境依赖的包</td>
<td><ruby>“webpack” : &quot;<rp>【</rp><rt>4.29.6&quot;</rt><rp>】</rp></ruby></td>
</tr>
</tbody>
</table>
</li>
</ul>
</li>
</ul>
<h3 id="node就遵循了这种模块化的设计"><a class="markdownIt-Anchor" href="#node就遵循了这种模块化的设计">#</a> node 就遵循了这种模块化的设计</h3>
<ul>
<li>将不同的功能，放在了不同的包 / 模块中</li>
<li>在需要用到的地方，直接引入使用即可</li>
</ul>
<h2 id="模块的分类"><a class="markdownIt-Anchor" href="#模块的分类">#</a> 模块的分类</h2>
<h3 id="核心模块"><a class="markdownIt-Anchor" href="#核心模块">#</a> 核心模块</h3>
<h4 id="概念-3"><a class="markdownIt-Anchor" href="#概念-3">#</a> 概念</h4>
<ul>
<li>因为有些功能是 Node 自带的，提供给我们使用</li>
<li>那把这一部分称作是 Node 的核心模块
<ul>
<li>fs</li>
<li>path</li>
<li>http</li>
<li>os</li>
<li>…</li>
</ul>
</li>
</ul>
<h4 id="特性"><a class="markdownIt-Anchor" href="#特性">#</a> 特性</h4>
<ul>
<li>已经被编译成二进制，无法查看源码</li>
</ul>
<h3 id="三方模块"><a class="markdownIt-Anchor" href="#三方模块">#</a> 三方模块</h3>
<ul>
<li>一般都是通过 npm/yarn 安装</li>
</ul>
<h3 id="自定义模块"><a class="markdownIt-Anchor" href="#自定义模块">#</a> 自定义模块</h3>
<h2 id="模块化实施规范"><a class="markdownIt-Anchor" href="#模块化实施规范">#</a> 模块化实施规范</h2>
<h3 id="解决的问题"><a class="markdownIt-Anchor" href="#解决的问题">#</a> 解决的问题</h3>
<ul>
<li>一个模块内的东西，如何导出去让别的模块使用</li>
<li>另一个模块，如何引入这个模块来使用</li>
</ul>
<h3 id="规范方案"><a class="markdownIt-Anchor" href="#规范方案">#</a> 规范方案</h3>
<h4 id="es6之前"><a class="markdownIt-Anchor" href="#es6之前">#</a> ES6 之前</h4>
<ul>
<li>并没有官方标准的模块化实施方案</li>
<li>在社区中产生了几个主流规范
<ul>
<li>AMD
<ul>
<li>代表框架 ---- RequireJS</li>
</ul>
</li>
<li>CMD
<ul>
<li>代表框架 ---- SeaJS</li>
</ul>
</li>
<li>CommonJS
<ul>
<li><code>module.exports</code>  导出</li>
<li><code>require()</code>  导入</li>
<li>注意
<ul>
<li>前端浏览器不支持，在后端 Node 使用</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="es6之后"><a class="markdownIt-Anchor" href="#es6之后">#</a> ES6 之后</h4>
<ul>
<li>之前的主流规范都没有采用</li>
<li>定义了新的规范
<ul>
<li>import</li>
<li>export</li>
</ul>
</li>
</ul>
<h4 id="commonjs模块使用规范"><a class="markdownIt-Anchor" href="#commonjs模块使用规范">#</a> CommonJS 模块使用规范</h4>
<ul>
<li>模块作用域
<ul>
<li>概念
<ul>
<li>文件作用域
<ul>
<li>Node 中，没法定义自己的全局作用域，只有模块作用域</li>
</ul>
</li>
<li>所以不需要担心一个文件中的变量，会被其他文件修改影响</li>
<li>外部访问不到内部，内部也访问不到外部</li>
</ul>
</li>
</ul>
</li>
<li>模块内向外导出
<ul>
<li>exports 使用
<ul>
<li>分开导出
<ul>
<li><code>exports.key = value</code></li>
</ul>
</li>
<li>统一导出
<ul>
<li><code>module.exports = 需要导出去的数据</code></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>使用方导入模块</li>
</ul>
<h2 id="核心模块如何使用"><a class="markdownIt-Anchor" href="#核心模块如何使用">#</a> 核心模块如何使用</h2>
<ul>
<li>核心模块为 node 安装时自带的，直接通过 <code>require</code>  引入使用即可</li>
</ul>
<h3 id="常用模块操作"><a class="markdownIt-Anchor" href="#常用模块操作">#</a> 常用模块操作</h3>
<h4 id="路径处理模块-path"><a class="markdownIt-Anchor" href="#路径处理模块-path">#</a> 路径处理模块 ： path</h4>
<ul>
<li>作用
<ul>
<li>处理各种路径问题</li>
</ul>
</li>
<li>常用 API
<ul>
<li>路径的拼接
<ul>
<li><code>path.join([path1][,path2]...)</code></li>
</ul>
</li>
<li>路径处理为绝对路径
<ul>
<li><code>path.resolve([from...,], to)</code></li>
</ul>
</li>
<li>获取一个路径的拓展名
<ul>
<li><code>path.extname(p)</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="文件操作模块-fs"><a class="markdownIt-Anchor" href="#文件操作模块-fs">#</a> 文件操作模块 fs</h4>
<ul>
<li>作用
<ul>
<li>可以使用此模块提供的 API 来读写文件</li>
<li>在 fs 模块中，所有的方法都分为同步和异步两种实现
<ul>
<li>有 sync 后缀的方法为同步方法</li>
<li>不具有 sync 后缀的方法为异步方法</li>
</ul>
</li>
</ul>
</li>
</ul>

            </div>

            
                <div class="post-copyright-info">
                    <div class="article-copyright-info-container">
    <ul>
        <li><strong>标题:</strong> Node.js</li>
        <li><strong>作者:</strong> meteor</li>
        <li><strong>创建于:</strong> 2023-06-05 17:02:34</li>
        
            <li>
                <strong>更新于:</strong> 2023-06-05 01:02:59
            </li>
        
        <li>
            <strong>链接:</strong> https://gitee.com/du-jinliang/2023/06/06/Node-js/
        </li>
        <li>
            <strong>版权声明:</strong> 本文章采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0</a> 进行许可。
        </li>
    </ul>
</div>

                </div>
            

            
                <ul class="post-tags-box">
                    
                        <li class="tag-item">
                            <a href="/tags/Node-js/">#Node.js</a>&nbsp;
                        </li>
                    
                </ul>
            

            

            
                <div class="article-nav">
                    
                        <div class="article-prev">
                            <a class="prev"
                            rel="prev"
                            href="/2023/06/06/Python/"
                            >
                                <span class="left arrow-icon flex-center">
                                    <i class="fa-solid fa-chevron-left"></i>
                                </span>
                                <span class="title flex-center">
                                    <span class="post-nav-title-item">Python</span>
                                    <span class="post-nav-item">上一篇</span>
                                </span>
                            </a>
                        </div>
                    
                    
                        <div class="article-next">
                            <a class="next"
                            rel="next"
                            href="/2023/06/06/Mybatis%E6%89%B9%E9%87%8F%E6%8F%92%E5%85%A5%E6%95%B0%E6%8D%AE%E7%9A%84%E4%B8%89%E7%A7%8D%E6%96%B9%E5%BC%8F%E6%95%88%E7%8E%87%E5%AF%B9%E6%AF%94/"
                            >
                                <span class="title flex-center">
                                    <span class="post-nav-title-item">Mybatis批量插入数据的三种方式效率对比</span>
                                    <span class="post-nav-item">下一篇</span>
                                </span>
                                <span class="right arrow-icon flex-center">
                                    <i class="fa-solid fa-chevron-right"></i>
                                </span>
                            </a>
                        </div>
                    
                </div>
            


            
                <div class="comment-container">
                    <div class="comments-container">
    <div id="comment-anchor"></div>
    <div class="comment-area-title">
        <i class="fa-solid fa-comments"></i>&nbsp;评论
    </div>
    

        
            
    <div id="gitalk-container"></div>
    <script data-pjax
            src="//cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.js"></script>
    <script data-pjax>

        function loadGitalk() {
            let __gitalk__pathname = decodeURI(location.pathname);
            const __gitalk__pathnameLength = __gitalk__pathname.length;
            const __gitalk__pathnameMaxLength = 50;
            if (__gitalk__pathnameLength > __gitalk__pathnameMaxLength) {
                __gitalk__pathname = __gitalk__pathname.substring(0, __gitalk__pathnameMaxLength - 3) + '...';
            }

            try {
                Gitalk && new Gitalk({
                    clientID: '55bad54a77b7e60ad62d',
                    clientSecret: '1031c81500c6be06e338087cb7b713f2d0201b46',
                    repo: 'wait-you.github.io',
                    owner: 'wait-you',
                    admin: ['wait-you'],
                    id: 'comment',
                    language: 'zh-CN'
                }).render('gitalk-container');

            } catch (e) {
                window.Gitalk = null;
            }
        }

        if ('true') {
            const loadGitalkTimeout = setTimeout(() => {
                loadGitalk();
                clearTimeout(loadGitalkTimeout);
            }, 1000);
        } else {
            window.addEventListener('DOMContentLoaded', loadGitalk);
        }
    </script>



        
    
</div>

                </div>
            
        </div>

        
            <div class="toc-content-container">
                <div class="post-toc-wrap">
    <div class="post-toc">
        <div class="toc-title">此页目录</div>
        <div class="page-title">Node.js</div>
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#nodejs"><span class="nav-text"> Node.js</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A6%82%E5%BF%B5"><span class="nav-text"> 概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9D%E4%BD%93%E9%AA%8C"><span class="nav-text"> 初体验</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86nvm"><span class="nav-text"> 版本管理 NVM</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="nav-text"> 常用命令</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#node%E5%85%A8%E5%B1%80%E7%9A%84%E8%AE%8A%E9%87%8F"><span class="nav-text"> Node 全局的變量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A6%82%E5%BF%B5-2"><span class="nav-text"> 概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F"><span class="nav-text"> 常用全局变量</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A8%A1%E5%9D%97%E5%92%8C%E5%8C%85%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text"> 模块和包的概念</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E5%9D%97%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text"> 模块的概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8C%85%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text"> 包的概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#node%E5%B0%B1%E9%81%B5%E5%BE%AA%E4%BA%86%E8%BF%99%E7%A7%8D%E6%A8%A1%E5%9D%97%E5%8C%96%E7%9A%84%E8%AE%BE%E8%AE%A1"><span class="nav-text"> node 就遵循了这种模块化的设计</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A8%A1%E5%9D%97%E7%9A%84%E5%88%86%E7%B1%BB"><span class="nav-text"> 模块的分类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%B8%E5%BF%83%E6%A8%A1%E5%9D%97"><span class="nav-text"> 核心模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%89%E6%96%B9%E6%A8%A1%E5%9D%97"><span class="nav-text"> 三方模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%9D%97"><span class="nav-text"> 自定义模块</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A8%A1%E5%9D%97%E5%8C%96%E5%AE%9E%E6%96%BD%E8%A7%84%E8%8C%83"><span class="nav-text"> 模块化实施规范</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A7%A3%E5%86%B3%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-text"> 解决的问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A7%84%E8%8C%83%E6%96%B9%E6%A1%88"><span class="nav-text"> 规范方案</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A0%B8%E5%BF%83%E6%A8%A1%E5%9D%97%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8"><span class="nav-text"> 核心模块如何使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97%E6%93%8D%E4%BD%9C"><span class="nav-text"> 常用模块操作</span></a></li></ol></li></ol></li></ol>

    </div>
</div>
            </div>
        
    </div>
</div>


                

            </div>
            
            

        </div>

        <div class="main-content-footer">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info">
            &copy;
            
              <span>2022</span>
              -
            
            2023&nbsp;&nbsp;<i class="fa-solid fa-heart fa-beat" style="--fa-animation-duration: 0.5s; color: #f54545"></i>&nbsp;&nbsp;<a href="/">meteor</a>
        </div>
        
            <script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    <span id="busuanzi_container_site_uv" class="busuanzi_container_site_uv">
                        访问人数&nbsp;<span id="busuanzi_value_site_uv" class="busuanzi_value_site_uv"></span>
                    </span>
                
                
                    <span id="busuanzi_container_site_pv" class="busuanzi_container_site_pv">
                        总访问量&nbsp;<span id="busuanzi_value_site_pv" class="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="theme-info info-item">
            <span class="powered-by-container">由 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1rem" height="1rem" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve"><path fill="#0E83CD" d="M256.4,25.8l-200,115.5L56,371.5l199.6,114.7l200-115.5l0.4-230.2L256.4,25.8z M349,354.6l-18.4,10.7l-18.6-11V275H200v79.6l-18.4,10.7l-18.6-11v-197l18.5-10.6l18.5,10.8V237h112v-79.6l18.5-10.6l18.5,10.8V354.6z"/></svg><a target="_blank" href="https://hexo.io">Hexo</a> 驱动</span>
                <br>
            <span class="theme-version-container">主题&nbsp;<a class="theme-version" target="_blank" href="https://github.com/EvanNotFound/hexo-theme-redefine">Redefine v2.1.4</a>
        </div>
        
            <div class="icp-info info-item"><a target="_blank" rel="nofollow" href="
                
                    beian.miit.gov.cn
                
                ">冀ICP备20010108号</a></div>
        
        
        
            <div id="start_div" style="display:none">
                2022/8/17 11:45:14
            </div>
            <div>
                博客已运行 <span class="odometer" id="runtime_days" ></span> 天 <span class="odometer" id="runtime_hours"></span> 小时 <span class="odometer" id="runtime_minutes"></span> 分钟 <span class="odometer" id="runtime_seconds"></span> 秒
            </div>
        
        
        
            <script async data-pjax>
                try {
                    function odometer_init() {
                    const elements = document.querySelectorAll('.odometer');
                    elements.forEach(el => {
                        new Odometer({
                            el,
                            format: '( ddd).dd',
                            duration: 200
                        });
                    });
                    }
                    odometer_init();
                } catch (error) {}
            </script>
        
        
        
    </div>  
</footer>
        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="article-tools-list">
        <!-- TOC aside toggle -->
        
            <li class="right-bottom-tools page-aside-toggle">
                <i class="fa-regular fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="go-comment">
                <i class="fa-regular fa-comments"></i>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-side-tools-container">
        <div class="side-tools-container">
    <ul class="hidden-tools-list">
        <li class="right-bottom-tools tool-font-adjust-plus flex-center">
            <i class="fa-regular fa-magnifying-glass-plus"></i>
        </li>

        <li class="right-bottom-tools tool-font-adjust-minus flex-center">
            <i class="fa-regular fa-magnifying-glass-minus"></i>
        </li>

        <li class="right-bottom-tools tool-expand-width flex-center">
            <i class="fa-regular fa-expand"></i>
        </li>

        <li class="right-bottom-tools tool-dark-light-toggle flex-center">
            <i class="fa-regular fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="right-bottom-tools tool-scroll-to-bottom flex-center">
            <i class="fa-regular fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="visible-tools-list">
        <li class="right-bottom-tools toggle-tools-list flex-center">
            <i class="fa-regular fa-cog fa-spin"></i>
        </li>
        
            <li class="right-bottom-tools tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
        
    </ul>
</div>

    </div>

    <div class="image-viewer-container">
    <img src="">
</div>


    


</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/layouts/navbarShrink.js"></script>

<script src="/js/tools/scrollTopBottom.js"></script>

<script src="/js/tools/lightDarkSwitch.js"></script>





    
<script src="/js/tools/codeBlock.js"></script>




    
<script src="/js/layouts/lazyload.js"></script>




    
<script src="/js/tools/runtime.js"></script>

    
<script src="/js/layouts/odometer.min.js"></script>

    
<link rel="stylesheet" href="/assets/odometer-theme-minimal.css">




  
<script src="/js/libs/Typed.min.js"></script>

  
<script src="/js/plugins/typed.js"></script>







<div class="post-scripts pjax">
    
        
<script src="/js/tools/tocToggle.js"></script>

<script src="/js/libs/anime.min.js"></script>

<script src="/js/layouts/toc.js"></script>

<script src="/js/plugins/tabs.js"></script>

    
</div>


    
<script src="/js/libs/pjax.min.js"></script>

<script>
    window.addEventListener('DOMContentLoaded', () => {
        window.pjax = new Pjax({
            selectors: [
                'head title',
                '.page-container',
                '.pjax',
            ],
            history: true,
            debug: false,
            cacheBust: false,
            timeout: 0,
            analytics: false,
            currentUrlFullReload: false,
            scrollRestoration: false,
            // scrollTo: true,
        });

        document.addEventListener('pjax:send', () => {
            Global.utils.pjaxProgressBarStart();
        });

        document.addEventListener('pjax:complete', () => {
            Global.utils.pjaxProgressBarEnd();
            window.pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
            Global.refresh();
        });
    });
</script>




</body>
</html>
